package vip.xiaonuo.smzq.modular.lifecycle.controller.module;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.K;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.common.pojo.CommonResult;

import vip.xiaonuo.smzq.modular.lifecycle.entity.*;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclCb06KcMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.KclWjglbMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.entity.LifeCycleMineStoreMapper;
import vip.xiaonuo.smzq.modular.lifecycle.mapper.module.LifeCycleMapper;
import vip.xiaonuo.smzq.modular.lifecycle.po.*;
import vip.xiaonuo.smzq.modular.lifecycle.po.datamanager.QueryMineListPO;
import vip.xiaonuo.smzq.modular.lifecycle.result.ResultWrapper;
import vip.xiaonuo.smzq.modular.lifecycle.result.WebResult;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclCb06KcService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclKsService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.KclZbService;
import vip.xiaonuo.smzq.modular.lifecycle.service.entity.LifeCycleMineStoreService;
import vip.xiaonuo.smzq.modular.lifecycle.service.factory.WrapperMeterFactory;
import vip.xiaonuo.smzq.modular.lifecycle.service.factory.produce.WrapperMeter;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.CommonService;
import vip.xiaonuo.smzq.modular.lifecycle.service.module.LifeCycleService;
import vip.xiaonuo.smzq.modular.lifecycle.util.CoordinateTranUtil;
import vip.xiaonuo.smzq.modular.lifecycle.vo.*;
import vip.xiaonuo.smzq.modular.xxgs.service.ILsksService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 矿山信息表的扩展
 *
 * @author geoscene_yuyanfei
 * @since 2024-03-20
 */
@Api(tags = "全生命周期模块")
@RestController
@RequestMapping("/life/cycle")
@Validated
public class LiftCycleController {

    @Autowired
    LifeCycleService lifeCycleService;

    @Autowired
    KclCb06KcMapper kclCb06KcMapper;

    @Autowired
    LifeCycleMapper  lifeCycleMapper;

    @Autowired
    KclWjglbMapper kclWjglbMapper;

    @Autowired
    LifeCycleMineStoreService lifeCycleMineStoreService;

    @Autowired
    KclZbService kclZbService;

    @Autowired
    ILsksService iLsksService;

    @Autowired
    CommonService commonService;

    @Autowired
    WrapperMeterFactory wrapperMeterFactory;

    @Resource
    CoordinateTranUtil coordinateTranUtil;

    @Resource
    ResultWrapper resultWrapper;

    @ApiOperation("根据行政区代码、年度查询矿山信息，根据矿业权类型分组")
    @PostMapping("/query/mineInfo")
    public CommonResult<List<QueryMineInfoRetVO>> queryMineInfo(@RequestBody QueryMineInfoPO queryMineInfoPO) {
        return CommonResult.data(lifeCycleService.queryMineInfo(queryMineInfoPO));
    }

    @ApiOperation("查询矿山信息列表")
    @PostMapping("/query/mineInfoList")
    public CommonResult<List<QueryMineInfoVO>> queryMineInfoList(@RequestBody QueryMineInfoPO queryMineInfoPO) {
        return CommonResult.data(lifeCycleService.queryMineInfoList(queryMineInfoPO,true));
    }

    @ApiOperation("查询矿山全生命周期列表(材料袋信息列表)")
    @PostMapping("/query/list")
    public WebResult queryList(@RequestBody QueryMineStorePO queryMineStorePO) {
        List<QueryMineStoreVO> list = lifeCycleService.queryLCNodeList(queryMineStorePO,true);
//        list = lifeCycleService.handerMerge(list);
        list = lifeCycleService.meterSameDateAndReportNameLCNodeList(list);
        list = wrapperMeterFactory.getProduce(WrapperMeter.revRecWrapperMeterServiceName).wrapper(list);
        return resultWrapper.getSuccessWebResult(list);
    }

    @ApiOperation("查询矿山全生命周期列表(材料袋信息列表)")
    @GetMapping("/query/byId")
    public WebResult queryList(@RequestParam("id")String id) {
        QueryMineStorePO queryMineStorePO = new QueryMineStorePO();
        queryMineStorePO.setId(id);
        List<QueryMineStoreVO> list = lifeCycleService.queryLifeCycleMineList(queryMineStorePO,false);
        return resultWrapper.getSuccessWebResult(CollUtil.getFirst(list));
    }

    @ApiOperation("根据登记分类编号得到需要显示到时间轴上的登记分类编号列表")
    @GetMapping("/query/getQueLcRegClassCodes")
    public WebResult getQueLcRegClassCodes(@RequestParam("regClassCode")Integer regClassCode) {

        return resultWrapper.getSuccessWebResult(lifeCycleService.getQueLcRegClassCodes(regClassCode));

    }






    @ApiOperation("查询矿山全生命周期列表(材料袋信息列表)")
    @PostMapping("/query/list/rs")
    public WebResult queryLifeCycleMineListRS(@RequestBody QueryMineStorePO queryMineStorePO) {
        return resultWrapper.getSuccessWebResult(lifeCycleService.queryLifeCycleMineListRS(queryMineStorePO));
    }
    @ApiOperation("查询矿山年度储量")
    @PostMapping("/query/year/store")
    public WebResult queryYearStore(@RequestBody QueryYearStorePO queryYearStorePO) {
        queryYearStorePO.setYear(null);
        return resultWrapper.getSuccessWebResult(lifeCycleService.staticsYearStore(queryYearStorePO));
    }

    @ApiOperation("根据矿石类型列表查询矿石列表")
    @PostMapping("/query/minerallistByCodes")
    public WebResult queryYearStore(@RequestBody List<QueryMeaListPO> queryMeaListPOS) {
        if(CollUtil.size(queryMeaListPOS)!=0){
            List<KclCb06Kc> result = CollUtil.newArrayList();
            List<Integer> codes = CollUtil.map(queryMeaListPOS,ele->ele.getKcdm(),true);
            List<KclCb06Kc> kclCb06Kcs = new LambdaQueryChainWrapper<KclCb06Kc>(kclCb06KcMapper).in(KclCb06Kc::getKcdm,codes).list();
            for (QueryMeaListPO queryMeaListPO:queryMeaListPOS) {
                for (KclCb06Kc kclCb06Kc:kclCb06Kcs) {
                    if(
                            ObjectUtil.equals(queryMeaListPO.getKcdm(),kclCb06Kc.getKcdm())
                            && ObjectUtil.equals(queryMeaListPO.getTjdx(),kclCb06Kc.getTjdx())
                            && ObjectUtil.equals(queryMeaListPO.getKclb(),kclCb06Kc.getKclb())
                    ){
                        result.add(kclCb06Kc);
                    }
                }
            }

            for (KclCb06Kc kclCb06Kc:result) {
                if(StrUtil.isNotBlank(kclCb06Kc.getJsdxmc())){
                    kclCb06Kc.setName(kclCb06Kc.getJsdxmc());
                }else{
                    kclCb06Kc.setName(kclCb06Kc.getKcmc());
                }
            }
            return resultWrapper.getSuccessWebResult(result);
        }

        return resultWrapper.getSuccessWebResult(CollUtil.newArrayList());
    }

    /*public WebResult queryYearStore(@RequestBody List<Integer> codes) {
        if(CollUtil.size(codes)!=0){
            return resultWrapper.getSuccessWebResult(new LambdaQueryChainWrapper<KclCb06Kc>(kclCb06KcMapper).in(KclCb06Kc::getKcdm,codes).list());
        }

        return resultWrapper.getSuccessWebResult(CollUtil.newArrayList());
    }*/
    @ApiOperation("根据矿山id查询该矿山所有的矿石类型字典")
    @PostMapping("/query/mineralTypeDict")
    public WebResult queryMineralTypeDict(@RequestBody QueryMineStorePO queryMineStorePO) {

        return resultWrapper.getSuccessWebResult(lifeCycleService.queryLifeCycleMineralTypeDict(queryMineStorePO));
    }

    @ApiOperation(value = "得到矿山的数量", notes = "得到矿山的数量")
    @GetMapping(value = "/queryMineCount")
    public WebResult queryMineCount(@RequestBody KclKs kclKs) {
        return resultWrapper.getSuccessWebResult(lifeCycleService.queryMineCount(kclKs));
    }

    @ApiOperation("查询矿山列表(将坐标转成84的)")
    @PostMapping("/query/mineList")
    public WebResult queryMineList(@RequestBody QueryMineListPO queryMineListPO) {
        return resultWrapper.getSuccessWebResult(lifeCycleService.queryMineListPage(queryMineListPO));
//        return resultWrapper.getSuccessWebResult(lifeCycleService.queryMineList(kclKs));
    }


    @ApiOperation("查詢行政区,并支持模糊匹配")
    @PostMapping("/query/administrativeRegion")
    public WebResult queryAdministrativeRegion(@RequestBody QueryAdministractiveRegionPO queryAdministractiveRegionPO) {
        return resultWrapper.getSuccessWebResult(lifeCycleService.queryMineRegionTree(queryAdministractiveRegionPO));
    }

    @ApiOperation("查询矿山坐标(矿区组坐标转成WGS84的)")
    @PostMapping("/query/mineCoordinate")
    public WebResult queryMineCoordinate(@RequestBody LifeCycleMineStore lifeCycleMineStore) {
        LifeCycleMineStore lifeCycleMineStoreRes = lifeCycleMineStoreService.getById(lifeCycleMineStore.getId());
        return resultWrapper.getSuccessWebResult(lifeCycleService.getLcNodCoor(lifeCycleMineStoreRes));
    }


    @ApiOperation("查询信息公示类型")
    @GetMapping("/query/xxgs")
    public WebResult queryXxgs(@RequestParam("xkzh")String xkzh) {

        return resultWrapper.getSuccessWebResult(iLsksService.getKsxxgslx(xkzh));
    }


    @ApiOperation("入库")
    @PostMapping("/insert/cld")
    public WebResult insertCLD(@RequestBody List<Dict> params) {
        List<String> list = CollUtil.newArrayList();
        for (Dict p:params) {
            List<String> ids = lifeCycleMapper.queryWJJIdList(p.getStr("date"));
            List<Map> pdfList = (List<Map>)p.getObj("pdfs");
            for (Map pdf:pdfList) {
                KclWjglb kclWjglb = new KclWjglb();
                kclWjglb.setZlscId(CollUtil.getFirst(ids));
                kclWjglb.setWjmc((String)pdf.get("title"));
                kclWjglb.setWjlj("d:\\a.txt");
                kclWjglbMapper.insert(kclWjglb);
            }


            System.out.println();
//            list.add(id);
//            System.out.println(CollUtil.getFirst(ids));
        }
        System.out.println(list);


        return resultWrapper.getSuccessWebResult(CollUtil.newArrayList());
    }
}
